package va.radiomaria.audio;

import java.io.File;
import java.io.IOException;

import org.farng.mp3.MP3File;
import org.farng.mp3.TagException;

import va.radiomaria.gui.DataModel;
import va.radiomaria.gui.LoggerInstance;

/**
 * ID3 Tag reader that recognizes different ID3 standards and chooses the
 * appropriate reader if available.
 */
public class GeneralID3Reader implements ID3Reader {

	ID3Reader specializedID3Reader = null;

	public GeneralID3Reader(File song) {
		try {
			MP3File mp3file = new MP3File(song);
			if (mp3file.hasID3v1Tag()) {
				specializedID3Reader = new ID3v1Reader(mp3file);
			} else if (mp3file.hasID3v2Tag()) {
				specializedID3Reader = new ID3v2Reader(mp3file);
			}

		} catch (IOException e) {
			DataModel.getInstance().addConsoleOutput(
					"Could not read tags for: " + song.getName(), LoggerInstance.AUDIO);
		} catch (TagException e) {
			DataModel.getInstance().addConsoleOutput(
					"Could not read tags for: " + song.getName(), LoggerInstance.AUDIO);
		}
	}

	public String getAlbumTitle() {
		try {
			if (specializedID3Reader != null) {
				return specializedID3Reader.getAlbumTitle();
			} else {
				return "unknown";
			}
		} catch (Exception e) {
			return "malformed tag";
		}

	}

	@Override
	public String getArtist() {
		try {
			if (specializedID3Reader != null) {
				return specializedID3Reader.getArtist();
			} else {
				return "unknown";
			}
		} catch (Exception e) {
			return "malformed tag";
		}
	}

	@Override
	public String getSongTitle() {
		try {
			if (specializedID3Reader != null) {
				return specializedID3Reader.getSongTitle();
			} else {
				return "unknown";
			}
		} catch (Exception e) {
			return "malformed tag";
		}
	}

	@Override
	public String getGenre() {
		try {
			if (specializedID3Reader != null) {
				return specializedID3Reader.getGenre();
			} else {
				return "unknown";
			}
		} catch (Exception e) {
			return "malformed tag";
		}
	}

}
